package com.boot.ys.mybatisplus.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.boot.ys.mybatisplus.config.RuoYiConfig;
import com.boot.ys.mybatisplus.entity.FileInfo;
import com.boot.ys.mybatisplus.result.Result;
import com.boot.ys.mybatisplus.service.FileUploadDownService;
import com.boot.ys.mybatisplus.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 文件上传接口
 * 需要在MultipartConfig类中配置最大上传大小
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class FileUploadDownController {


    @Autowired
    private FileUploadDownService fileUploadDownService;

    @PostMapping("/upload/file")
    public String upload(@RequestParam("file") MultipartFile multipartFile) {
        // 1：空判断
        if (multipartFile.isEmpty()) {
            return "文件为空！！！";
        }
        multipartFile.getSize();//得到大小
        multipartFile.getOriginalFilename();//得到文件名
        String contentType = multipartFile.getContentType();//得到文件类型
        // 2：判断文件是否符合
//        if ("image/png".equals(contentType) || !"image/jpg".equals(contentType) || !"image/jpeg".equals(contentType)) {
//            return "文件格式不符合";
//        }
        return fileUploadDownService.uploadImg(multipartFile);
    }


    /**
     * 封装一个上传文件到服务器的方法，这个是参考数藏平台的若依框架的平台
     */
    @PostMapping("upFile")
    public Result upFile(MultipartFile file){
        String filePath = RuoYiConfig.getUploadPath();
        String myFilePath = filePath + "/goods";
        try {
            String fileName = fileUploadDownService.upFile(myFilePath, file);
            return Result.ok(fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.ok();
    }






}
